﻿/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using PsTFS.Methodes;
using PsTFS.Provider;
using PsTFS.Objects;

namespace PsTFS.Commandes.Add
{
    /// <summary>
    /// Création d'un workspace
    /// </summary>
    [Cmdlet(VerbsCommon.Add, "Workspace")]
    public class AddWorkspace : PsCmdlet
    {
        /// <summary>
        /// Lancement du processus de création de workspace
        /// </summary>
        protected override void ProcessRecord()
        {
            try
            {
	            var workspace = MethodService.VersionControlServer()
		            .CreateWorkspace(Name, Owner, null, WorkspaceMappings);
	            Context.Instance.Workspace = workspace;
				WriteObject(workspace);
            }
            catch (Exception ex)
            {
				var err = ErrorRecord(ex, ErrorCategory.WriteError);
				WriteError(err);
                Log.AddLog(ex.StackTrace);
            }
            finally
            {
                base.ProcessRecord();
            }

        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            Log.AddLog(null, "Add-Workspace is end");
            base.EndProcessing();
        }

        /// <summary>
        /// Object contenant Team Foundation Server
        /// </summary>
        [Parameter(Position = 0)]
        public TfsTeamProjectCollection TFS
        {
            set { Context.Instance.TFS = value; }
        }
        /// <summary>
        /// Nom du workspace
        /// </summary>
        [Parameter(Position = 1)]
		[ValidateNotNullOrEmpty]
		public string Name { get; set; }

        /// <summary>
        /// Propriétaire du workspace
        /// </summary>
        [Parameter(Position = 2)]
		[ValidateNotNullOrEmpty]
		public string Owner { get; set; }

		[Parameter(Position = 3)]
		[ValidateNotNullOrEmpty]
		//public string[][] WorkspaceMappings { get; set; }
		public WorkingFolder[] WorkspaceMappings { get; set; }
    }
}